package com.juneng;
import com.alibaba.fastjson.JSONObject;
import com.juneng.config.RedisUtil;
import com.juneng.constant.YsVideoConstant;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName YsVideoT
 * @Author 方东东
 * @Date 2021/5/24 9:58 下午
 * @Description YsVideoT
 */
public class YsVideoT {

    public static void main(String[] args) {


        RestTemplate restTemplate = new RestTemplate();
        RedisUtil  redisUtil = new RedisUtil();
        final String YsTokenApi = "https://open.ys7.com/api/lapp/token/get";
        final String GetVideoListApi = "https://open.ys7.com/api/lapp/live/video/list";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String getTokenUrl = YsTokenApi + "?appKey="+YsVideoConstant.YsAppKey +"&appSecret="+YsVideoConstant.YsSecret;
        System.out.println(getTokenUrl);
        HttpEntity<String> httpEntity = new HttpEntity<>(null, headers);
        String body = restTemplate.exchange(getTokenUrl, HttpMethod.POST, httpEntity, String.class).getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = (JSONObject) jsonObject.get("data");
        String  accessToken = (String) data.get("accessToken");
        long expireTime = (Long) data.get("expireTime")/1000;
        //存储token
        redisUtil.set("YsAccessToken",accessToken,expireTime);
//        System.out.println(date);
        System.out.println(body);
    }
}
